*** VARIOUS PROGRAMS TO RUN ANALYSIS ************  /*No support is given to these programs. Developed for private use by Eduardo L LEoni.tlevel Estimates separate models in each unit and delivers the dataset with estimates and standard errors to the userdropc drop countries dropcountries drop countriesrvars rename variables and merge databorjas second step regression using Borjas weightssetgraph  set the values to draw the graphminmax auxiliary program to set the values to do the graphsdograph does the graphs*/    cap prog drop dograph  prog define dograph    local ind="`1'"  local var1="`ind'min"  local var2="`ind'max"  local title="`3'"  local title2="`4'"  local maxind "max`ind'"  local minind "min`ind'"  local max`ind'=round($`maxind' * 10,1)/10  local line "(line mean`var1' mean`var2' x)"  local varlabelind="varlabel`ind'"  local legend `"3 `"$`varlabelind' = $`minind'"' 4 `"$`varlabelind' = `max`ind''"'"'    graph twoway (rcap min`var1' max`var1' x) (rcap min`var2' max`var2' x) (scatter mean`var1' mean`var2' x) `line'   , legend( order("`legend'")) saving(`2', replace) $groptions title("`title'" "`title2'", span)   endcap prog drop setgraphprog def setgraphlocal ind="`1'"global varlabel`ind': variable label `ind'  sum `ind'local minind="min`ind'"local maxind="max`ind'"global `minind'=round((r(mean)-r(sd)),.1)global `maxind'=round((r(mean)+r(sd)),.1)local diff=(r(mean)+r(sd))-(r(mean)-r(sd))sum `ind'replace `ind'=`ind'-(r(mean)-r(sd))sum `ind'local varlist "var`ind'"global vars12c "$`varlist'"global min="min`ind'min"global mean="mean`ind'min"global max="max`ind'min"minmaxreplace `ind'=`ind'-`diff'sum `ind'global min="min`ind'max"global mean="mean`ind'max"global max="max`ind'max"minmaxend**implemented borjas weight  ell2002@columbia.edu October 2004 using edvreg.ado by Jeffrey Lewis **   *** STATA COMMAND: edvreg 1.1**** Implements two-stage estimators described in****       Jeffrey Lewis, "Estimating Regression Models**       in which the Dependent Variable Is Based on Estimates..."**       Unpublished working paper, 2000.****** SYNTAX: edvreg <varlist>, dvste(<varname>) [prop]**   where,**     varlist = list of variables as in the reg command.**     varname = standard errors of the dependent variable.**     prop    = Option tells Stata that <varname> is proportional,**               rather than equal, to the SE of the dependent variable.******  (C) Jeffrey B Lewis (jblewis@Princeton.edu), August 2000**capture program drop borjasprogram define borjas   version 6.0   tempvar svar dvv varCorr   syntax varlist [if] [in], Dvste(string) [Prop]   local indvars "`varlist'"   gettoken depvar indvars : indvars   qui gen `dvv' = `dvste'*`dvste' `if'`in'   disp   disp in green "Sampled dependent variable regression"   disp   if ("`prop'" == "") {      qui gen `svar' = sum(`dvv')      local swsq = `svar'[_N]      qui reg `depvar' `indvars' `if'`in'      local dof = e(df_r)      local sumsr = `dof'*e(rmse)*e(rmse)      qui matrix accum XpX  = `indvars' `if' `in'      qui matrix accum XpGX = `indvars' [iw=`dvv'] `if' `in'      matrix tr = trace(inv(XpX)*XpGX)      *      following line modified by E. leoni      *      local sigma2 = (`sumsr'-`swsq'-tr[1,1]) / `dof'       local sigma2 = (`sumsr'-`swsq') / `dof'            if (`sigma2' < 0) {local sigma2 = 0}      disp in green "Estimated Sigma     = " in yellow %7.5f sqrt(`sigma2')      disp in green "Mean Omega2         = " in yellow %7.5f sqrt(`swsq'/`dof')            qui gen `varCorr' = 1/(`sigma2'+`dvv') `if'`in'      regress `depvar' `indvars'  `if'`in' [aw=`varCorr']   }   else {     tempvar e v      qui reg `depvar' `indvars'  `if'`in'      predict `e' if e(sample), resid      replace `e' = `e'*`e'      qui reg `e' `dvv' `if'`in', robust      if (_b[_cons]>0) {         disp in green "Coeff. on variance from sampling = " _continue         disp in yellow %7.4f _b[`dvv'] " (" %7.4f _se[`dvv'] ")"         disp in green "   Estimated variance of epsilon = " _continue         disp in yellow %7.4f _b[_cons] " (" %7.4f _se[_cons] ")"      }      else {         qui reg `e' `dvv' `if'`in', robust nocons         disp in green "Coeff. on variance from sampling = " _continue         disp in yellow %7.4f _b[`dvv'] " (" %7.4f _se[`dvv'] ")"         disp in green "             Variance of epsilon = " _continue         disp in yellow %7.4f 0 in blue " (constraint imposed)"      }      predict `v' if e(sample)      regress `depvar' `indvars'  `if'`in' [aw=1/`v']   }endcap prog drop rvarscap drop _mergeprog def rvarssort A1006merge A1006 using macrorename no_p noprename ln_w lnwrename seat_ seatdrop belgiumgen belgium=A1006==561label var A2001 "mean age"label var A2003  "mean education"endcap prog drop dropcprog def dropckeep if dem>=9&dem<.***drop some countriesif `1'==1 {	*drop belgium	drop if A1006==561	}*drop thailanddrop if A1006==7640cap drop if _merge==2*drop Germany newdrop if A1006==2762*drop Portugal new***Australia!local australia=1if `australia'==0 {  noi di "***Australia is being dropped from analysis***"  drop if A1006==360}endcap prog drop minmaxprog def minmaxgen $min=.gen $max=.gen $mean=.forval i=1($skip)$maxlevel1 {  borjas b_cons`i' $vars12c, dvste(se_cons`i')  cap  replace $min=_b[_cons]-2*_se[_cons] in `i'  cap  replace $max=_b[_cons]+2*_se[_cons] in `i'  cap replace $mean=_b[_cons] in `i'  *noi di _b[_cons]  *noi di exp(_b[_cons])/(1+exp(_b[_cons]))}endcap prog drop loopprog define loopif "`3'"=="" {    local skip="1"} else {    local skip="`3'"}forval i=1(`skip')`2' {  tempfile results`i'  preserve  replace `1'=`1'-`i'  capture noisily tlevel (simple $vars), command(probit) options(robust) wt([aw=A1010_1]) saving(`results`i'') by(A1006)   restore}use `results1', cleargen base=1local second=1+`skip'forval i=`second'(`skip')`2' {  append using `results`i''  replace base=`i' if base==.}drop if base==.reshape wide b_* se_* r2 t_* N, i(A1006) j(base)sort A1006*merge A1006 using macro*drop if _merge==2cap drop _mergeendforeach progtodrop in tlevel parseEqn nameEq Matches {  cap prog drop `progtodrop'}program define tlevelversion 7.0quietly {  /*  Parse (y1 x1 x2) (y2 y1 x2 x3) structure.*  Pick up full varlist (flist), y-array (y`i'), indendent*  variables (ind`i') left-hand  sides (lhslist), *  equation names (eqnm`i') */  local neq = 0/*  Parse the equations ([eqname:] y1 [y2 y3 =] x1 x2 x3) *  and fill in the structures required for estimation */    gettoken fulleq 0 : 0, parse(" ,[") match(paren)IsStop `fulleq'while `s(stop)' == 0 {   if "`paren'" != "(" {		/* must be eq */				eq ?? `fulleq' 				local fulleq "`r(eqname)': `r(eq)'"                              }   parseEqn `fulleq'    /* Set-up equation bookeeping structures */    local flist `flist' `depvars' `indvars'  tokenize `depvars'  local i 1  while "``i''" != "" {    local neq = `neq' + 1    local y`neq' ``i''    local lhslist `lhslist' ``i''    local ind`neq' `indvars'    local cons`neq' = ("`constan'" == "")    nameEq "`eqname'" "``i''" "`eqlist'" `neq'    local eqnm`neq' = "`s(eqname)'"    local eqlist `eqlist' `s(eqname)'    local i = `i' + 1  }    gettoken fulleq 0 : 0, parse(" ,[") match(paren)  IsStop `fulleq'}local 0 `"`fulleq' `0'"'if `neq' < 1 {   di in red "equation(s) required"   exit 198}syntax [if] [in] , COMmand(string) BY(varlist) SAving(string asis) [Wt(string asis)] [OPTions(string asis)] [Ftest(varlist)]**** TO DO***  check if basevars numeric**** TO DOlocal bcommand=substr("`command'",1,3) /* get the command stub - we should test the various models and insert appropriate remarks here. */  if "`bcommand'"==`"reg"' { /* If the command is a regression */  local r2="r2_a" /*get adjusted R2 */    local r2l="Adjusted R2" /*get label for pseudo R2 to use later */    }  else { /*if there is no adjusted R2 */  local r2="r2_p" /*get pseudo R2 - check with other models */    local r2l="Pseudo R2" /*get label for pseudo R2 to use later */    }tempvar unitegen `unit'=group(`by')sum `unit'local U=r(max)*loop over modelsforval j=1/`neq' {	  tempfile f`j' /* create a temporary file name to store results of model j*/  preserve    gen r2=.  gen N=.  if "`ind`j''"~="" {	  foreach var of varlist `ind`j'' {		  pause		  gen b_`var'=.		  gen se_`var'=.		  }	  }	  gen b_cons=.	  gen se_cons=.  gen F=.  	  	  forval u=1/`U' {		  di in red "`command' `y`j'' `ind`j'' `in' `wt' if `unit'==`u', `options'"		  		  `command' `y`j'' `ind`j'' `in' `wt' if `unit'==`u', `options'    		  if "`ind`j''"~="" {			  foreach var of varlist `ind`j'' {				  replace b_`var'=_b[`var'] if `unit'==`u'				  replace se_`var'=_se[`var']  if `unit'==`u'				  replace r2=e(`r2')  if `unit'==`u'				  replace N=e(N)  if `unit'==`u'				  }                        }                  if "`ftest'"~="" {                    test `ftest'                    replace F=r(p)  if `unit'==`u'                  }                  replace b_cons=_b[_cons] if `unit'==`u'                  replace se_cons=_se[_cons] if `unit'==`u'                                  }/*      qui  statsby `"`command' `y`j'' `ind`j'' `if' `in' "' e(N) _b[] _se[] r2=e(`r2'), by(`by') saving("`f`j''")*/pausecollapse b_* se_* r2 N F, by(`by')save "`f`j''"restore}quietly{  local Nmodels=`neq'    preserve  use "`f1'", clear /* use the file with the first model results */    gen str50 depvar="`y1'" /* generate a variable with the dependnet var of model 1 */      gen str80 indvars="`ind1'" /* gen a variable with the model 1 variables */        gen byte modeln=1 /* gen a model number variable */          gen str80 ifstatement="`if'"  gen str80 instatement="`in'"    forval i=2/`neq' { /* for each model after 1 */  append using "`f`i''" /*append data */    /* replace model, modeln, depvar with the new values */      replace indvars="`ind`i''"  if indvars==""                      replace modeln=`i' if modeln==.                     replace depvar="`y`i''" if depvar==""}      foreach b of varlist b_* {    local var: subinstr local b "b_" "" /*take the b_ stub out of the variable (to get original name) */      local varlist `" `varlist' b_`var' se_`var' t_`var' "' /* create a macro with the variable list */        gen t_`var'=`b'/se_`var' /* generate t stat */        }  gen str20 cmd="`command'" /* command (logit, reg, etc) */    order `by'  modeln model cmd depvar r2   `varlist' /* order the data nicely */                  /*label the variables */                label var r2 "`r2l'" /*assign label to R2 variable */          label var depvar "dependent var."  label var modeln "model number"label var cmd "command"label var model "estimated model"compresssort modeln `by'}order `by' modeln cmd depvar indvars r2save "`saving'", replacerestoreendcap prog drop IsStopprogram define IsStop, sclass	if 	     `"`0'"' == "[" /*		*/ | `"`0'"' == "," /*		*/ | `"`0'"' == "if" /*		*/ | `"`0'"' == "in" /*		*/ | `"`0'"' == "" {		sret local stop 1	}	else	sret local stop 0endprogram define parseEqn        	/* see if we have an equation name */	gettoken token uu : 0, parse(" =:")   /* rare, pull twice if found */	gettoken token2 : uu, parse(" =:")     /* rare, pull twice if found */	if index("`token2'", ":") != 0 {		gettoken token  0 : 0, parse(" =:")      /* sic, to set 0 */		gettoken token2 0 : 0, parse(" =:")      /* sic, to set 0 */		c_local eqname  `token'	} 	else    c_local eqname 	/* search just for "=" */	gettoken token 0 : 0, parse(" =")	while "`token'" != "=" & "`token'" != "" {		local depvars `depvars' `token'		gettoken token 0 : 0, parse(" =")	}	if "`token'" == "=" {		tsunab depvars : `depvars'		syntax [varlist(ts)] [ , noConstant ]	} 	else {				/* assume single depvar */		local 0 `depvars'		syntax varlist(ts) [ , noConstant ]		gettoken depvars varlist : varlist	}	c_local depvars `depvars'	c_local indvars `varlist'	c_local constan `constan'end/*  determine equation name */program define nameEq, sclass	args	    eqname	/* user specified equation name		*/  depvar	/* dependent variable name		*/  eqlist	/* list of current equation names 		*/  neq		/* equation number */		if "`eqname'" != "" {		if index("`eqname'", ".") {di in red "may not use periods (.) in equation names: `eqname'"		}		local eqlist : subinstr local eqlist "`eqname'" "`eqname'", /*			*/ word count(local count)    /* overkill, but fast */		if `count' > 0 {di in red "may not specify duplicate equation names: `eqname'"			exit 198		}		sreturn local eqname `eqname'		exit	}		local depvar : subinstr local depvar "." "_", all	if length("`depvar'") > 32 {		local depvar "eq`neq'"	}	Matches dupnam : "`eqlist'" "`depvar'"	if "`dupnam'" != "" {		sreturn local eqname = substr("`neq'`depvar'", 1, 32)	}	else {		sreturn local eqname `depvar'	}end                /*  Returns tokens found in both lists in the macro named by matches. *  Duplicates must be duplicated in both lists to be considered *  matches a 2nd, 3rd, ... time.  */    program define Matches   args	    matches     /*  macro name to hold cleaned list*/  colon	/*  ":"*/  list1	/*  a list of tokens*/  list2	/*  a second list of tokens */    tokenize `list1'local i 1while "``i''" != "" {          local list2 : subinstr local list2 "``i''" "", /*			*/ word count(local count)		if `count' > 0 {                  local matlist `matlist' ``i''                }		local i = `i' + 1              }c_local `matches' `matlist'endcap prog drop dropcountriesprog define dropcountries*drop belgium-walloon (**no simple/strong pid information)drop if A1006==562/* Drop Hong Kong */*****************************************drop if ctrycode==102*belarusdrop if ctrycode==31*mexicodrop if ctrycode==14&year==1997*perudrop if ctrycode==51*leave only one survey per country*the latest in mod1 and the mod2 for countries not in mod1drop if A1006==3480&A1008~=1998drop if A1006==5540&A1008~=1996drop if A1006==6160&A1008~=1997drop if A1006==6430&A1008~=2000drop if A1006==7240&A1008~=2000drop if A1006==1580&A1008==2001drop if A1006==2030&A1008==2002drop if A1006==3760&A1008==2003drop if A1006==5780&A1008==2001drop if A1006==7520&A1008==2002drop if A1006==7560&A1008==2003*this is germany 2002drop if A1006==2761*drop wave 2 of Portugal (it is the same survey as in wave 1)drop if A1006==6200&A1001=="CSES-MODULE-2"end